package com.Thread;

import java.util.concurrent.*;

/**
 * @author: sun.hongliang
 * @create: 2019/07/02 18:32
 */
public class CyclicBarrierTest {
    public static void main(String[] args) {
        CyclicBarrier cyclicBarrier = new CyclicBarrier(4);
        final ExecutorService executorService = Executors.newFixedThreadPool(5);
        System.out.println("main start .....");
        executorService.execute(new Task(cyclicBarrier, "a"));
        executorService.execute(new Task(cyclicBarrier, "b"));
        executorService.execute(new Task(cyclicBarrier, "c"));
        executorService.execute(new Task(cyclicBarrier, "d"));
        System.out.println("main end ......");
    }
}

class Task implements Runnable {
    private CyclicBarrier cyclicBarrier;
    private String name;

    public Task(CyclicBarrier cyclicBarrier, String name) {
        this.cyclicBarrier = cyclicBarrier;
        this.name = name;
    }

    @Override
    public void run() {
        System.out.println(name + "start....");
        try {
            cyclicBarrier.await(5000, TimeUnit.MILLISECONDS);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(name + "end....");
    }
}
